package com.dmooo.yxmmerchant.view.presenter;

import android.content.Context;

import com.common.mvpbase.BasePresenter;
import com.common.net.listener.HttpOnNextListener;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.domain.BannerListBean;
import com.dmooo.yxmmerchant.view.contract.BannerContract;
import com.dmooo.yxmmerchant.view.model.BannerModel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class BannerPrestener extends BasePresenter<BannerContract.BannerView> implements BannerContract.BannerPtr {
    private BannerModel model;
    private GsonBuilder builder=new GsonBuilder();
    public BannerPrestener(BannerContract.BannerView mView, Context context) {
        super(mView);
        model=new BannerModel(context);
        builder.serializeNulls();
    }

    @Override
    public void getBannerList(String merchant_id) {
        model.getBannerList(merchant_id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                LogUtil.d("dsafds",o.toString());
                try {
                    JSONObject obj=new JSONObject(builder.create().toJson(o));
                    mView.shwoBannerList(new Gson().fromJson(obj.toString(),BannerListBean.class).list);
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void delBanner(String token, String id, final int position) {
        model.delBanner(token, id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.delBannerSuccess(position);
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void addBanner(String token, File file, String title, String type, String type_value, String href, String sort, String is_show) {
        model.addBanner(token, file, title, type, type_value, href, sort, is_show, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.addBannerSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void editBanner(String token, String id, File file, String title, String type, String type_value, String href, String sort, String is_show) {
        model.editBanner(token, id, file, title, type, type_value, href, sort, is_show, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.editBannerSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
